/**
 * Created by xcold on 2016/1/29
 */
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ParkSchema = new Schema({
    car_controller_type: {
        type: Number
    },
    parkId: {
        type: Number
    },
    name: {
        type: String,
    },
    deviceNo: {
        type: Number
    },
    longitude: {
        type: Number
    },
    latitude: {
        type: Number
    },
    spaceCount: {
        type: Number
    },
    allowBookCount: {
        type: Number
    },
    parkType: {
        type: Number
    },
    cooperation: {
        type: Boolean
    },
    price: {
        type: Number
    },
    linkman: {
        type: String
    },
    linkphone: {
        type: Number
    },
    address: {
        type: String
    }
});
var Park = mongoose.model('Park', ParkSchema);
module.exports = function() {
    /**
     * 添加停车场
     */
    var addPark = function(parkInfo) {
        Park.create(parkInfo,
            function(err, doc) {
                if (err) throw err;
            }
        );
    };
    
    /**
     * 删除停车场
     */
    var delPark = function(parkId, callback) {
        Park.find().remove({'parkId': parkId}, function (err, doc) {
            if (err) throw err;
            callback(err, doc);
        })
    };

    /**
     * 获取停车场列表
     */
    var getParkList = function(index, callback) {
        var pageSize = 12;  // 每一页的停车场数量
        Park.find({}, {
            _id: 0,
            name: 1,
            car_controller_type: 1,
            parkId: 1,
            deviceNo: 1,
            addreess: 1,
            longitude: 1,
            latitude: 1,
            spaceCount: 1,
            allowBookCount: 1,
            parkType: 1,
            price: 1,
            address: 1
        }, {
            skip: (index - 1) * pageSize,
            limit: pageSize
        }, function(err, doc) {
            if (err) throw err;
            callback(err, doc);
        });
    };

    /**
     *  关键字获取停车场
     */
    var getParkListByKey = function(index, query, callback) {
        var query = new RegExp(query, 'i');
        var pageSize = 12;  // 每一页的停车场数量
        var index = index || 1;
        Park.find({
                '$or': [{
                    'name': query
                }, {
                    'address': query
                }]
            }, {
                _id: 0,
                name: 1,
                car_controller_type: 1,
                parkId: 1,
                deviceNo: 1,
                addreess: 1,
                longitude: 1,
                latitude: 1,
                spaceCount: 1,
                allowBookCount: 1,
                parkType: 1,
                price: 1,
                address: 1
            }, {
                skip: (index - 1) * pageSize,
                limit: pageSize
            },
            function(err, doc) {
                if (err) throw err;
                callback(err, doc);
            })
    }

    /**
     *  关键字获取停车场
     */
    var getTotalCount = function(callback) {
        Park.count(function(err, count) {
            console.log(count)
            if (err) throw err;
            callback(err, count);
        });
    };


    return {
        addPark: addPark,
        delPark: delPark,
        getParkList: getParkList,
        getTotalCount: getTotalCount,
        getParkListByKey: getParkListByKey,
        Park: Park
    }

};